cd "~/Dropbox/Shared Folder - J. Ladd & M (1). Meredith/Replication Files/"
cap log close
log using "LogFiles/TableA8.log", replace

clear all
set more off

matrix Results = J(21, 9, -9)

use "RawData/GallupDataBJPS.dta", clear

drop if drops == "All" 

// Sets the Survey Date as the Mid-Point in Field
gen middle_date = .
replace middle_date = floor((start_date + end_date) / 2)

gen presidentid = 1 if middle_date < mdy(1, 20, 1961) 
replace presidentid = 2 if middle_date >= mdy(1, 20, 1961) & middle_date < mdy(1, 20, 1969)
replace presidentid = 3 if middle_date >= mdy(1, 20, 1969) & middle_date < mdy(1, 20, 1977) 
replace presidentid = 4 if middle_date >= mdy(1, 20, 1977) & middle_date < mdy(1, 20, 1981)
replace presidentid = 5 if middle_date >= mdy(1, 20, 1981) & middle_date < mdy(1, 20, 1989)
replace presidentid = 6 if middle_date >= mdy(1, 20, 1989) & middle_date < mdy(1, 20, 1993) 
replace presidentid = 7 if middle_date >= mdy(1, 20, 1993) & middle_date < mdy(1, 20, 2001) 
replace presidentid = 8 if middle_date >= mdy(1, 20, 2001) & middle_date < mdy(1, 20, 2009) 
replace presidentid = 9 if middle_date >= mdy(1, 20, 2009) 

gen Job_as_President = 1
replace Job_as_President = 0 if pres_approve <= -9 | regexm(drops, "Approval")

gen Handling_of_Economy = 1
replace Handling_of_Economy = 0 if pres_approve_economy <= -9 | regexm(drops, "Approval")

gen Handling_of_Foreign_Affairs = 1
replace Handling_of_Foreign_Affairs = 0 if pres_approve_foreign <= -9  | regexm(drops, "Approval")

gen Consider_Yourself = 1
replace Consider_Yourself = 0 if party <= -9 | regexm(drops, "Party")

gen Lean_More_to = 1
replace Lean_More_to = 0 if party2 <= -9  | regexm(drops, "Party")

gen Ideology = 1
replace Ideology = 0 if ideo <= -9  | regexm(drops, "Ideology")

gen Female = 1
replace Female = 0 if female <= -9 

gen Black = 1
replace Black = 0 if black <= -9 

gen Hispanic = 1
replace Hispanic = 0 if hispanic <= -9 

gen Age = 1
replace Age = 0 if age <= -9 

gen Education = 1
replace Education = 0 if education <= -9 

gen Married = 1
replace Married = 0 if married <= -9 

gen Religion = 1
replace Religion = 0 if religion <= -9 

gen Union_Household = 1
replace Union_Household = 0 if unionHH <= -9 

gen Employment = 1
replace Employment = 0 if employment <= -9 

gen Chief_Wage_Earner_Industry = 1
replace Chief_Wage_Earner_Industry = 0 if industry <= -9 

gen Income = 1
replace Income = 0 if lower_bound_income <= -9 & missing_income ~= 1

gen State_of_Residence = 1
replace State_of_Residence = 0 if state <= -9 

gen Area_Code = 1
replace Area_Code = 0 if area <= -9 

gen Congressional_District = 1
replace Congressional_District = 0 if cd <= -9 

gen City_Size = 1
replace City_Size = 0 if lower_bound_citysize <= -9 

forvalues i = 1(1)9 {
local j = 1
foreach var of varlist Job_as_President-City_Size {
qui sum `var' if  presidentid == `i'
matrix Results[`j', `i'] = round(100*r(mean), 1)
local j = `j' + 1
}
}

matlist Results

log close
